<?php

namespace app\api\controller\v1;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Controller;
use think\Request;
use app\common\lib\exception\ApiException;
use tegic\qiniu\Qiniu;
class Index extends Controller
{	
	
	public function transferTable(){
		$params = $_GET;
		if($params['t']==2 && empty($params['id'])){
			echo json_encode(['code'=>1,'msg'=>'无数据','data'=>[],'count'=>0]);die;
		}
		if(!empty($params['id'])){
			$ids = explode(',', $params['id']);
		}else{
			$ids = [];
		}
		$data = [];
		for ($i=1; $i < 100 ; $i++) {
			$sex = $i%2==0?'男':'女';
			if($params['t']==1 && !in_array($i, $ids)){
				if(!empty($params['sex']) && $params['sex']==1 && $sex=='男'){
					$data[] = ['id'=>$i,'username'=>'user_'.$i,'sex'=>$sex];
				}else if(!empty($params['sex']) && $params['sex']==2 && $sex=='女'){
					$data[] = ['id'=>$i,'username'=>'user_'.$i,'sex'=>$sex];
				}else if(empty($params['sex'])){
					$data[] = ['id'=>$i,'username'=>'user_'.$i,'sex'=>$sex];
				}
				
			}else if($params['t']==2 && in_array($i, $ids)){
				$data[] = ['id'=>$i,'username'=>'user_'.$i,'sex'=>$sex];
			}
			
		}
		if($data){
			$arr = array_chunk($data,$params['limit'])[$params['page']-1];
			echo json_encode(['code'=>0,'msg'=>'','data'=>$arr,'count'=>count($data)]);
		}else{
			echo json_encode(['code'=>1,'msg'=>'无数据','data'=>[],'count'=>0]);
		}
	} 
	/**
	    * 上传文件
	*/
	public function up()
	    {
	        $file = $this->request->file('file');
	        if (empty($file)) {
				return show(config('code.error'), '未上传文件或超出服务器上传限制', $e->getMessage(), 200);
	        }
	        $fileInfo = $file->getInfo();
	
	        $infoArr         = explode('.', $fileInfo['name']);
	        $extension       = array_pop($infoArr);
	        $saveName        = hash_file('sha1', $fileInfo['tmp_name']) . time();
	        $saveName        = $saveName . '.' . $extension;
	
	        // 需要填写你的 Access Key 和 Secret Key
	        $accessKey =config('app.qiniu.accesskey');
	        $secretKey = config('app.qiniu.secretkey');
	        $bucket = config('app.qiniu.bucket');
	        // 构建鉴权对象
	        $auth = new Auth($accessKey, $secretKey);
	        // 生成上传 Token
	        $token = $auth->uploadToken($bucket);
	        // 初始化 UploadManager 对象并进行文件的上传。
	        $uploadMgr = new UploadManager();
	        // 调用 UploadManager 的 putFile 方法进行文件的上传。
	        
	        
			try {
			    list($result, $e) = $uploadMgr->putFile($token, $saveName, $fileInfo['tmp_name']);
				if($result['key']){
					$url = config('app.qiniu.url').$result['key'];
					return show(config('code.success'), 'OK', $url, 200);
				}
			} catch (Exception $e) {
				return show(config('code.error'), 'error', $e->getMessage(), 200);
			}
	
	    }

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
    public function index()
    {
        $cate = model('Category')->column('id,pid,name');
        $data = [];
        foreach ($cate as $key=>$v) {
            $data[$key]['id'] = $v['id'];
            $data[$key]['pid'] = $v['pid'];
            $data[$key]['pId'] = $v['pid'];
            $data[$key]['name'] = $v['name'];
            $data[$key]['open'] = true;
        }
        $result = [
            "cate" =>$data
        ];
        return show(config('code.success'), 'OK', $result, 200);
    }
    
    public function cate()
    {
        $cate = model('Category')->select();
        $result = [
            "cate" =>$cate
        ];
        return show(config('code.success'), 'OK', $result, 200);
    }

    public function test()
    {
        config('code.prefix');
    }
    
    public function notice()
    {
        $data = [
                [
                    'id'=> "000000002",
                    'avatar' => "https://gw.alipayobjects.com/zos/rmsportal/OKJXDXrmkNshAMvwtvhu.png",
                    'title' =>"你推荐的 曲妮妮 已通过第三轮面试",
                    'datetime' =>"2017-08-08",
                    'type' =>"通知"
                ]
            ];
        return show(config('code.success'), 'OK', $data, 200);
    }
    
    public function user()
    {
        $data = [
            "name"=> "皮皮瞎",
            "avatar"=>"https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png",
            "userid"=>"00000001",
            "notifyCount"=> 12
        ];
        return show(config('code.success'), 'OK', $data, 200);
    }
    public function addModule()
    {
        $data = request()->param();
        $rest = [];
        $rest['type'] = $data['type'];
        $rest['content'] = json_encode($data['content'], true);
        $rest['thumb'] = $data['thumb'];
        $data = db('bancard')->insert($rest);
        return show(config('code.success'), 'OK', $data, 200);
    }
    public function getModule()
    {
        $data = db('bancard')->find();
        $data['content'] = json_decode($data['content']);
        return show(config('code.success'), 'OK', $data, 200);
    }
    public function addImages()
    {
        $file = request()->file('imgFile');
        $url = 'uploads/thumb';
        $info = $file->move($url);
        if ($info) {
            $filename = str_replace('\\', '/', $info->getSaveName());
            $rest = 'http://'.$_SERVER['HTTP_HOST'].'/'.$url.'/'.$filename;
            return upshow(0, $rest, 200);
        } else {
            // 上传失败获取错误信息
            return upshow(1, $file->getError(), 200);
        }
    }
    public function thumb()
    {
        $file = request()->file('file');
        $url = 'uploads/thumb';
        $info = $file->move($url);
        if ($info) {
            $filename = str_replace('\\', '/', $info->getSaveName());
            $rest = 'http://'.$_SERVER['HTTP_HOST'].'/'.$url.'/'.$filename;
            return upshow(0, $rest, 200);
        } else {
            // 上传失败获取错误信息
            return upshow(1, $file->getError(), 200);
        }
    }
    public function userInfo()
    {
        $data = [
            'id'=>'4291d7da9005377ec9aec4a71ea837f',
            'name'=>'王小玉',
            'username'=>'admin',
            'password'=>'',
            'avatar'=>'/avatar2.jpg',
            'status'=>1,
            'telephone'=>'',
            'lastLoginIp'=>'27.154.74.117',
            'lastLoginTime'=>1534837621348,
            'creatorId'=>'admin',
            'createTime'=>1497160610259,
            'merchantCode'=>'TLif2btpzg079h15bk',
            'deleted'=>0,
            'roleId'=> 'admin',
            'role'=> []
        ];
        
        
        $role = [
          'id'=>'admin',
          'name'=>'管理员',
          'describe'=>'拥有所有权限',
          'status'=>1,
          'creatorId'=>'system',
          'createTime'=>1497160610259,
          'deleted'=>0,
          'permissions'=>[[
            'roleId'=>'admin',
            'permissionId'=>'dashboard',
            'permissionName'=>'仪表盘',
            'actions'=>[
                    ["action"=>"add","defaultCheck"=>false,"describe"=>"新增"],["action"=>"query","defaultCheck"=>false,"describe"=>"查询"],["action"=>"get","defaultCheck"=>false,"describe"=>"详情"],["action"=>"update","defaultCheck"=>false,"describe"=>"修改"],["action"=>"delete","defaultCheck"=>false,"describe"=>"删除"]],
            'actionEntitySet'=>[[
              'action'=>'add',
              'describe'=>'新增',
              'defaultCheck'=>false
            ],[
              'action'=>'query',
              'describe'=>'查询',
              'defaultCheck'=>false
            ],[
              'action'=>'get',
              'describe'=>'详情',
              'defaultCheck'=>false
            ],[
              'action'=>'update',
              'describe'=>'修改',
              'defaultCheck'=>false
            ],[
              'action'=>'delete',
              'describe'=>'删除',
              'defaultCheck'=>false
            ]],
            'actionList'=>null,
            'dataAccess'=>null
          ],[
            'roleId'=>'admin',
            'permissionId'=>'exception',
            'permissionName'=>'异常页面权限',
            'actions'=>[["action"=>"add","defaultCheck"=>false,"describe"=>"新增"],["action"=>"query","defaultCheck"=>false,"describe"=>"查询"],["action"=>"get","defaultCheck"=>false,"describe"=>"详情"],["action"=>"update","defaultCheck"=>false,"describe"=>"修改"],["action"=>"delete","defaultCheck"=>false,"describe"=>"删除"]],
            'actionEntitySet'=>[[
              'action'=>'add',
              'describe'=>'新增',
              'defaultCheck'=>false
            ],[
              'action'=>'query',
              'describe'=>'查询',
              'defaultCheck'=>false
            ],[
              'action'=>'get',
              'describe'=>'详情',
              'defaultCheck'=>false
            ],[
              'action'=>'update',
              'describe'=>'修改',
              'defaultCheck'=>false
            ],[
              'action'=>'delete',
              'describe'=>'删除',
              'defaultCheck'=>false
            ]],
            'actionList'=>null,
            'dataAccess'=>null
          ],[
            'roleId'=>'admin',
            'permissionId'=>'result',
            'permissionName'=>'结果权限',
            'actions'=>[["action"=>"add","defaultCheck"=>false,"describe"=>"新增"],["action"=>"query","defaultCheck"=>false,"describe"=>"查询"],["action"=>"get","defaultCheck"=>false,"describe"=>"详情"],["action"=>"update","defaultCheck"=>false,"describe"=>"修改"],["action"=>"delete","defaultCheck"=>false,"describe"=>"删除"]],
            'actionEntitySet'=>[[
              'action'=>'add',
              'describe'=>'新增',
              'defaultCheck'=>false
            ],[
              'action'=>'query',
              'describe'=>'查询',
              'defaultCheck'=>false
            ],[
              'action'=>'get',
              'describe'=>'详情',
              'defaultCheck'=>false
            ],[
              'action'=>'update',
              'describe'=>'修改',
              'defaultCheck'=>false
            ],[
              'action'=>'delete',
              'describe'=>'删除',
              'defaultCheck'=>false
            ]],
            'actionList'=>null,
            'dataAccess'=>null
          ],[
            'roleId'=>'admin',
            'permissionId'=>'profile',
            'permissionName'=>'详细页权限',
            'actions'=>[["action"=>"add","defaultCheck"=>false,"describe"=>"新增"],["action"=>"query","defaultCheck"=>false,"describe"=>"查询"],["action"=>"get","defaultCheck"=>false,"describe"=>"详情"],["action"=>"update","defaultCheck"=>false,"describe"=>"修改"],["action"=>"delete","defaultCheck"=>false,"describe"=>"删除"]],
            'actionEntitySet'=>[[
              'action'=>'add',
              'describe'=>'新增',
              'defaultCheck'=>false
            ],[
              'action'=>'query',
              'describe'=>'查询',
              'defaultCheck'=>false
            ],[
              'action'=>'get',
              'describe'=>'详情',
              'defaultCheck'=>false
            ],[
              'action'=>'update',
              'describe'=>'修改',
              'defaultCheck'=>false
            ],[
              'action'=>'delete',
              'describe'=>'删除',
              'defaultCheck'=>false
            ]],
            'actionList'=>null,
            'dataAccess'=>null
          ],[
            'roleId'=>'admin',
            'permissionId'=>'table',
            'permissionName'=>'表格权限',
            'actions'=>[["action"=>"add","defaultCheck"=>false,"describe"=>"新增"],["action"=>"import","defaultCheck"=>false,"describe"=>"导入"],["action"=>"get","defaultCheck"=>false,"describe"=>"详情"],["action"=>"update","defaultCheck"=>false,"describe"=>"修改"]],
            'actionEntitySet'=>[[
              'action'=>'add',
              'describe'=>'新增',
              'defaultCheck'=>false
            ],[
              'action'=>'import',
              'describe'=>'导入',
              'defaultCheck'=>false
            ],[
              'action'=>'get',
              'describe'=>'详情',
              'defaultCheck'=>false
            ],[
              'action'=>'update',
              'describe'=>'修改',
              'defaultCheck'=>false
            ]],
            'actionList'=>null,
            'dataAccess'=>null
          ],[
            'roleId'=>'admin',
            'permissionId'=>'form',
            'permissionName'=>'表单权限',
            'actions'=>[["action"=>"add","defaultCheck"=>false,"describe"=>"新增"],["action"=>"get","defaultCheck"=>false,"describe"=>"详情"],["action"=>"query","defaultCheck"=>false,"describe"=>"查询"],["action"=>"update","defaultCheck"=>false,"describe"=>"修改"],["action"=>"delete","defaultCheck"=>false,"describe"=>"删除"]],
            'actionEntitySet'=>[
                [
              'action'=>'add',
              'describe'=>'新增',
              'defaultCheck'=>false
            ],[
              'action'=>'get',
              'describe'=>'详情',
              'defaultCheck'=>false
            ],[
              'action'=>'query',
              'describe'=>'查询',
              'defaultCheck'=>false
            ],[
              'action'=>'update',
              'describe'=>'修改',
              'defaultCheck'=>false
            ],[
              'action'=>'delete',
              'describe'=>'删除',
              'defaultCheck'=>false
            ]],
            'actionList'=>null,
            'dataAccess'=>null
          ],[
            'roleId'=>'admin',
            'permissionId'=>'order',
            'permissionName'=>'订单管理',
            'actions'=>[["action"=>"add","defaultCheck"=>false,"describe"=>"新增"],["action"=>"query","defaultCheck"=>false,"describe"=>"查询"],["action"=>"get","defaultCheck"=>false,"describe"=>"详情"],["action"=>"update","defaultCheck"=>false,"describe"=>"修改"],["action"=>"delete","defaultCheck"=>false,"describe"=>"删除"]],
            'actionEntitySet'=>[[
              'action'=>'add',
              'describe'=>'新增',
              'defaultCheck'=>false
            ],[
              'action'=>'query',
              'describe'=>'查询',
              'defaultCheck'=>false
            ],[
              'action'=>'get',
              'describe'=>'详情',
              'defaultCheck'=>false
            ],[
              'action'=>'update',
              'describe'=>'修改',
              'defaultCheck'=>false
            ],[
              'action'=>'delete',
              'describe'=>'删除',
              'defaultCheck'=>false
            ]],
            'actionList'=>null,
            'dataAccess'=>null
          ],[
            'roleId'=>'admin',
            'permissionId'=>'permission',
            'permissionName'=>'权限管理',
            'actions'=>[
                    [
                        "action"=>"add",
                        "defaultCheck"=>false,
                        "describe"=>"新增"
                    ],
                    [
                        "action"=>"get",
                    "defaultCheck"=>false,
                    "describe"=>"详情"],["action"=>"update","defaultCheck"=>false,"describe"=>"修改"],["action"=>"delete","defaultCheck"=>false,"describe"=>"删除"]],
            'actionEntitySet'=>[[
              'action'=>'add',
              'describe'=>'新增',
              'defaultCheck'=>false
            ],[
              'action'=>'get',
              'describe'=>'详情',
              'defaultCheck'=>false
            ],[
              'action'=>'update',
              'describe'=>'修改',
              'defaultCheck'=>false
            ],[
              'action'=>'delete',
              'describe'=>'删除',
              'defaultCheck'=>false
            ]],
            'actionList'=>null,
            'dataAccess'=>null
          ],[
            'roleId'=>'admin',
            'permissionId'=>'role',
            'permissionName'=>'角色管理',
            'actions'=>[
                    [
                        "action"=>"add",
                        "defaultCheck"=>false,
                        "describe"=>"新增"
                    ],
                    [
                        "action"=>"get",
                        "defaultCheck"=>false,
                        "describe"=>"详情"
                    ],
                    [
                        "action"=>"update",
                        "defaultCheck"=>false,
                        "describe"=>"修改"
                    ],
                    [
                            "action"=>"delete",
                            "defaultCheck"=>false,
                            "describe"=>"删除"
                    ]
            ],
            'actionEntitySet'=>[[
              'action'=>'add',
              'describe'=>'新增',
              'defaultCheck'=>false
            ],[
              'action'=>'get',
              'describe'=>'详情',
              'defaultCheck'=>false
            ],[
              'action'=>'update',
              'describe'=>'修改',
              'defaultCheck'=>false
            ],[
              'action'=>'delete',
              'describe'=>'删除',
              'defaultCheck'=>false
            ]],
            'actionList'=>null,
            'dataAccess'=>null
          ],[
            'roleId'=>'admin',
            'permissionId'=>'table',
            'permissionName'=>'桌子管理',
            'actions'=>[["action"=>"add","defaultCheck"=>false,"describe"=>"新增"],["action"=>"get","defaultCheck"=>false,"describe"=>"详情"],["action"=>"query","defaultCheck"=>false,"describe"=>"查询"],["action"=>"update","defaultCheck"=>false,"describe"=>"修改"],["action"=>"delete","defaultCheck"=>false,"describe"=>"删除"]],
            'actionEntitySet'=>[[
              'action'=>'add',
              'describe'=>'新增',
              'defaultCheck'=>false
            ],[
              'action'=>'get',
              'describe'=>'详情',
              'defaultCheck'=>false
            ],[
              'action'=>'query',
              'describe'=>'查询',
              'defaultCheck'=>false
            ],[
              'action'=>'update',
              'describe'=>'修改',
              'defaultCheck'=>false
            ],[
              'action'=>'delete',
              'describe'=>'删除',
              'defaultCheck'=>false
            ]],
            'actionList'=>null,
            'dataAccess'=>null
          ],[
            'roleId'=>'admin',
            'permissionId'=>'user',
            'permissionName'=>'用户管理',
            'actions'=>[["action"=>"add","defaultCheck"=>false,"describe"=>"新增"],["action"=>"import","defaultCheck"=>false,"describe"=>"导入"],["action"=>"get","defaultCheck"=>false,"describe"=>"详情"],["action"=>"update","defaultCheck"=>false,"describe"=>"修改"],["action"=>"delete","defaultCheck"=>false,"describe"=>"删除"],["action"=>"export","defaultCheck"=>false,"describe"=>"导出"]],
            'actionEntitySet'=>[[
              'action'=>'add',
              'describe'=>'新增',
              'defaultCheck'=>false
            ],[
              'action'=>'import',
              'describe'=>'导入',
              'defaultCheck'=>false
            ],[
              'action'=>'get',
              'describe'=>'详情',
              'defaultCheck'=>false
            ],[
              'action'=>'update',
              'describe'=>'修改',
              'defaultCheck'=>false
            ],[
              'action'=>'delete',
              'describe'=>'删除',
              'defaultCheck'=>false
            ],[
              'action'=>'export',
              'describe'=>'导出',
              'defaultCheck'=>false
            ]],
            'actionList'=>null,
            'dataAccess'=>null
          ]]
        ];
        $data['role'] = $role;
        return show(config('code.success'), 'OK', $data, 200);
    }
    
    public function version()
    {
        $data = [
            "name"=>"Xiaopeng_PC",
            "version"=> "1.9.2",
            "changeLogs"=>['来个开发祭天','新增了🐂🍺的功能','新增课堂列表和小组界面','修改了若干bug，优化了好多问题'],
            "time"=>'2019-06-06',
            "asar"=>"https://c.uxuj.cn/update/update.zip",
            "downloadUrl"=>"https://xiaopengpc.oss-cn-beijing.aliyuncs.com/xiaopeng-1.9-Setup.exe"
        ];
        return json($data);
    }
}
